#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n;
ll a[15], b[15];
ll B[15], IB[15], P;
ll gcd(ll a, ll b, ll& x, ll& y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll d = gcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}
ll inv(ll a, ll p) {
  ll x, y;
  gcd(a, p, x, y);
  x %= p;
  if (x < 0) x += p;
  return x;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) cin >> b[i];
  rep(i, 1, n) {
    a[i] %= b[i];
    if (a[i] < 0) a[i] += b[i];
  }
  rep(i, 1, n) {
    B[i] = 1;
    rep(j, 1, n) {
      if (j == i) continue;
      B[i] *= b[j];
    }
  }
  P = B[1] * b[1];
  ll x = 0;
  rep(i, 1, n) {
    __int128_t invb = inv(B[i], b[i]);
    x = (x + invb * a[i] * B[i] % P) % P;
  }
  cout << x;
  return 0;
}